package me.project.jobster.service;

import me.project.jobster.common.constant.GlobalConstants;
import me.project.jobster.dao.mybatis.mapper.CompanyMapper;
import me.project.jobster.dao.mybatis.mapper.StudentMapper;
import me.project.jobster.dao.mybatis.model.Company;
import me.project.jobster.dao.mybatis.model.Student;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Mcdull
 * @date 2018-4-27
 */
@Service
public class UserService {

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * update student profile by primary key
     * @param userId
     * @param name
     * @param university
     * @param major
     * @param gpa
     * @param gpaStatus
     */
    public void updateStudentProfile(String userId, String name, String university,
                              String major,String gpa, String gpaStatus, String resume){
        Student s = new Student();
        s.setsId(userId);
        Integer primaryId = studentMapper.selectOne(s).getId();
        Student student = new Student();
        student.setId(primaryId);
        student.setsId(userId);
        student.setName(name);
        student.setUniversity(university);
        student.setMajor(major);
        student.setGpa(gpa);
        student.setGpaStatus(gpaStatus);
        student.setResume(resume);
        studentMapper.updateByPrimaryKeySelective(student);

    }

    public List<Student> getStudentList(){
        List<Student> list = studentMapper.selectAll();
        for (Student student : list) {
            if (GlobalConstants.GPA_FLAG_INVISIBLE.equals(student.getGpaStatus())){
                student.setGpa("--");
            }
        }
        return list;
    }

    /**
     * select student by userId
     * @param userId
     * @return
     */
    public Student getOwnerInfo(String userId){
        Student student = new Student();
        student.setsId(userId);
        return studentMapper.selectOne(student);
    }
    /**
     * select student by userId
     * @param userId
     * @return
     */
    public Student getStudentInfo(String userId){
        Student student = new Student();
        student.setsId(userId);
        Student student1 = studentMapper.selectOne(student);
        if (GlobalConstants.GPA_FLAG_INVISIBLE.equals(student1.getGpaStatus())){
            student1.setGpa("--");
        }
        return student1;
    }

    /**
     * fuzzy search
     * @param student
     * @param gpaSearchFlag
     * @return
     */
    public List<Student> getStudentListFuzzy(Student student,String gpaSearchFlag){
        StringBuilder sql = new StringBuilder("select * from student where 1=1 ");
        List <Object> queryList=new ArrayList<Object>();
        if (!StringUtils.isEmpty(student.getName())){
            sql.append(" and name like concat('%',?,'%')");
            queryList.add(student.getName().trim());
        }
        if (!StringUtils.isEmpty(student.getEmail())){
            sql.append(" and email like concat('%',?,'%')");
            queryList.add(student.getEmail().trim());
        }
        if (!StringUtils.isEmpty(student.getMajor())){
            sql.append(" and major like concat('%',?,'%')");
            queryList.add(student.getMajor().trim());
        }
        if (!StringUtils.isEmpty(student.getUniversity())){
            sql.append(" and university like concat('%',?,'%')");
            queryList.add(student.getUniversity().trim());
        }
        if (!StringUtils.isEmpty(student.getGpa())){
            if ("1".equals(gpaSearchFlag)){
                sql.append(" and gpa = ? ");
            }else if ("2".equals(gpaSearchFlag)){
                sql.append(" and gpa < ? ");
            }else if ("3".equals(gpaSearchFlag)){
                sql.append(" and gpa > ? ");
            } else {
                sql.append(" and gpa like concat('%',?,'%')");
            }
            queryList.add(student.getGpa().trim());
        }
        if (!StringUtils.isEmpty(student.getResume())){
            sql.append(" and resume like concat('%',?,'%')");
            queryList.add(student.getResume().trim());
        }
        List<Student> list = jdbcTemplate.query(sql.toString(),queryList.toArray(),new BeanPropertyRowMapper<>(Student.class));
        for (Student s : list) {
            if (GlobalConstants.GPA_FLAG_INVISIBLE.equals(s.getGpaStatus())){
                student.setGpa("--");
            }
        }
        return list;
    }

    //--------------------------company-----------------------------//

    public void updateCompanyProfile(String userId, String name, String location,
                                     String headquarter,String industry, String remark){
        Company c = new Company();
        c.setcId(userId);
        Integer primaryId = companyMapper.selectOne(c).getId();
        Company company = new Company();
        company.setId(primaryId);
        company.setcId(userId);
        company.setName(name);
        company.setLocation(location);
        company.setIndustry(industry);
        company.setHeadquarter(headquarter);
        company.setRemark(remark);
        companyMapper.updateByPrimaryKeySelective(company);

    }
    /**
     * get all list
     * @return
     */
    public List<Company> getCompanyList(){
        return companyMapper.selectAll();
    }

    /**
     * select company by userId
     * @param userId
     * @return
     */
    public Company getCompanyInfo(String userId){
        Company company = new Company();
        company.setcId(userId);
        return companyMapper.selectOne(company);
    }

    /**
     * fuzzy search
     * @param company
     * @return
     */
    public List<Company> getCompanyListFuzzy(Company company){
        StringBuilder sql = new StringBuilder("select * from company where 1=1 ");
        List <Object> queryList=new ArrayList<Object>();
        if (!StringUtils.isEmpty(company.getName())){
            sql.append(" and name like concat('%',?,'%')");
            queryList.add(company.getName().trim());
        }
        if (!StringUtils.isEmpty(company.getEmail())){
            sql.append(" and email like concat('%',?,'%')");
            queryList.add(company.getEmail().trim());
        }
        if (!StringUtils.isEmpty(company.getLocation())){
            sql.append(" and location like concat('%',?,'%')");
            queryList.add(company.getLocation().trim());
        }
        if (!StringUtils.isEmpty(company.getHeadquarter())){
            sql.append(" and headquarter like concat('%',?,'%')");
            queryList.add(company.getHeadquarter().trim());
        }
        if (!StringUtils.isEmpty(company.getIndustry())){
            sql.append(" and industry like concat('%',?,'%')");
            queryList.add(company.getIndustry().trim());
        }
        if (!StringUtils.isEmpty(company.getRemark())){
            sql.append(" and remark like concat('%',?,'%')");
            queryList.add(company.getRemark().trim());
        }
        return jdbcTemplate.query(sql.toString(),queryList.toArray(),new BeanPropertyRowMapper<>(Company.class));

    }

}
